package net.alcuria.ball;

import net.alcuria.framework.Pixmap;

public class Sprite {
	// Variables and their default values
	int stripIndex = 0;
	int frameIndex = 0;
	int framesInStrip = 1;
	int updatesPerFrame = 3; // Arbitrary
	int updatesCounter = updatesPerFrame;
	int frameWidth = 36;
	int frameHeight = 36;
	int locX = 0;
	int locY = 0;
	boolean looping = true;
	
	Pixmap spritesheet;
	int srcX, srcY;
	
	public Sprite(Pixmap spritesheet, int frameWidth, int frameHeight) {
		this.spritesheet = spritesheet;
		this.frameWidth = frameWidth;
		this.frameHeight = frameHeight;
		
		framesInStrip = spritesheet.getWidth() / frameWidth;
		
		srcX = frameIndex * frameWidth;
		srcY = stripIndex * frameHeight;
	}
	
	public void update(float deltaTime) {
		/* Incorporate this logic sometime in the future:
		 * Update frame if time since last update is greater than time per frame */
		updatesCounter++;
		
		if(updatesCounter >= updatesPerFrame){
			// Update the frame
			frameIndex++;
			if(frameIndex >= framesInStrip && looping){
				frameIndex = 0;
			}
			updatesCounter = 0;
			
			// Update the srcX
			srcX = frameIndex * frameWidth;
			
		}
	}
	
	public void setStrip(int stripIndex) {
		this.stripIndex = stripIndex;
		srcY = stripIndex * frameHeight;
		
		if(srcY >= spritesheet.getHeight()) {
			// Indexing a strip that doesn't exist
			stripIndex = 0;
			srcY = stripIndex * frameHeight;
			
		}
	}

}
